<template>
  <el-dialog
      :title="!dataForm.id ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible"
      fullscreen>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="120px">
      <el-form-item label="所属房屋" prop="selectedHouse">
        <el-cascader clearable filterable :options="houseTree"
                     v-model="dataForm.selectedHouse" placeholder="请选择房屋"></el-cascader>
      </el-form-item>
      <el-form-item label="是否可用" prop="available">
        <el-switch
            v-model="dataForm.available"
            active-color="#13ce66"
            inactive-color="#ff4949">
        </el-switch>
      </el-form-item>
      <el-form-item label="设备类型" prop="type">
        <el-select v-model="dataForm.type" placeholder="请选择设备类型">
          <el-option
              v-for="item in types"
              :key="item.key"
              :label="item.label"
              :value="item.key">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备型号" prop="model">
        <el-input v-model="dataForm.model" placeholder="设备型号" class="inputSpace"></el-input>
      </el-form-item>
      <el-form-item label="生产厂商" prop="manufacturer">
        <el-input v-model="dataForm.manufacturer" placeholder="生产厂商" class="inputSpace"></el-input>
      </el-form-item>
      <el-form-item label="购买日期" prop="purchaseDate">
        <el-date-picker
            v-model="dataForm.purchaseDate"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="设备到期时间" prop="expireDate">
        <el-date-picker
            v-model="dataForm.expireDate"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="备注" prop="note">
        <el-input v-model="dataForm.note" placeholder="备注" class="inputSpace"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      types: [
        {key: 1, label: '灭火器'},
        {key: 2, label: '烟雾探测器'},
      ],
      visible: false,
      //房屋的树形结构
      houseTree: [],
      dataForm: {
        id: 0,
        //[buildingId,floorId,houseId]
        selectedHouse: [],
        available: true,
        type: '',
        model: '',
        manufacturer: '',
        purchaseDate: '',
        expireDate: '',
        note: ''
      },
      dataRule: {
        selectedHouse: [
          {required: true, message: '所属房屋不能为空', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '设备类型不能为空', trigger: 'blur'}
        ],
        expireDate: [
          {required: true, message: '设备到期时间不能为空', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    init(id) {
      //获取房屋的树形结构
      this.getHouseTree()
      console.log('init:', id)
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$request({
            url: `/device/device/info/${this.dataForm.id}`,
            method: 'get',
            params: {}
          }).then(res => {
            if (res && res.code === 200) {
              this.dataForm = res.device
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$request({
            url: `/device/device/${!this.dataForm.id ? 'save' : 'update'}`,
            method: this.dataForm.id ? 'put' : 'post',
            data: {
              ...this.dataForm,
              id: this.dataForm.id || null
            }
          }).then(res => {
            if (res && res.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },
    //获取房屋的树形结构
    getHouseTree() {
      this.$request.get('/house/house/getHouseTree').then(res => {
        if (res.code === 200) {
          this.houseTree = res.data
        } else {
          this.$message.error('获取房屋信息失败')
        }
      })
    },
  }
}
</script>

<style scoped>
.inputSpace {
  width: 80%;
}
</style>
